<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Crm_Line_Apply extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'crm_line_apply';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $fillable = [
        'agencyid',
        'mid',
        'otherid',
        'status',
        'is_del'
    ];

    protected $hidden = [
        'is_del',
    ];

    public function scopeNodel($query)
    {
        return $query->where('is_del', 'N');
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function hasOneMember()
    {
        return $this->hasOne('App\Models\Crm_Member', 'id', 'mid');
    }

    public function hasOneOtherMember()
    {
        return $this->hasOne('App\Models\Crm_Member', 'id', 'otherid');
    }

    public static function getFilter($filter)
    {
        $model = self::query();
        if ($filter['type'] == 3 && !empty($filter['keyword'])) {
            $model->whereHas('hasOneMember' , function ($query) use ($filter){
                $query->where('username', 'LIKE', '%' . $filter['keyword'] . '%');
            });
        }
        if ($filter['type'] == 4 && !empty($filter['keyword'])) {
            $model->whereHas('hasOneOtherMember' , function ($query) use ($filter){
                $query->where('username', 'LIKE', '%' . $filter['keyword'] . '%');
            });
        }
        if ($filter['type'] == 5 && !empty($filter['keyword'])) {
            $model->whereHas('hasOneMember' , function ($query) use ($filter){
                $query->where('mobile', 'LIKE', '%' . $filter['keyword'] . '%');
            });
        }
        if ($filter['type'] == 6 && !empty($filter['keyword'])) {
            $model->whereHas('hasOneOtherMember' , function ($query) use ($filter){
                $query->where('mobile', 'LIKE', '%' . $filter['keyword'] . '%');
            });
        }
        $model->where(function ($query) use ($filter){
            if ($filter['type'] == 1 && !empty($filter['keyword'])) {
                $query->where('mid',  $filter['keyword'] );
            }
            if ($filter['type'] == 2 && !empty($filter['keyword'])) {
                $query->where('otherid',  $filter['keyword'] );
            }
            if (!empty($filter['status'])) {
                $query->where('status',  $filter['status'] );
            }
            $query->where('agencyid',  $filter['aid'] );
        });

        return $model;
    }
}
